package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.*;
import com.example.communitypro.pojo.*;
import com.example.communitypro.service.UserCollectionService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCollectionServiceImpl implements UserCollectionService {
    @Autowired(required=false)
    private UserCollectionMapper userCollectionMapper;
    @Autowired(required=false)
    private CommunityInformationMapper communityInformationMapper;
    @Autowired(required=false)
    private PostMapper postMapper;
    @Autowired(required=false)
    private CommentMapper commentMapper;
    @Autowired(required=false)
    private LabelRelationMapper labelRelationMapper;
    @Autowired(required=false)
    private LabelMapper labelMapper;

    @Override
    public Result addUserCollection(UserCollection record){
        try {
            if(userCollectionMapper.insertSelective(record)>0)
            {
                if (record.getCollectionObjectType()==0){
                   CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                   int now = communityInformation.getCommunityCollectionNumber()+1;
                   communityInformation.setCommunityCollectionNumber(now);
                   communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                }
                if (record.getCollectionObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now =post.getPostCollectionNumber()+1;
                    post.setPostCollectionNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                }
                if (record.getCollectionObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = comment.getCommentCollectionNumber()+1;
                    comment.setCommentCollectionNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                return Result.ok().message("新增用户收藏成功");
            }
            else{
                return Result.error().message("新增用户收藏失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户收藏失败");
        }
    }


    @Override
    public Result dropUserCollection(int collectionNo){
        try {
            if(userCollectionMapper.selectByPrimaryKey(collectionNo)!=null)
            {
                UserCollection record = userCollectionMapper.selectByPrimaryKey(collectionNo);

                if (record.getCollectionObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = communityInformation.getCommunityCollectionNumber()-1;
                    communityInformation.setCommunityCollectionNumber(now);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);

                }
                if (record.getCollectionObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now =post.getPostCollectionNumber()-1;
                    post.setPostCollectionNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                }
                if (record.getCollectionObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = comment.getCommentCollectionNumber()-1;
                    comment.setCommentCollectionNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                userCollectionMapper.deleteByPrimaryKey(collectionNo);
                return Result.error().message("删除用户收藏成功");
            }
            else{
                return Result.error().message("删除用户收藏失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户收藏失败");
        }
    }

    @Override
    public Result modifyUserCollection(UserCollection record){
        try {
            if(userCollectionMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserCollection(int collectionNo){
        try {
            UserCollection userCollection=userCollectionMapper.selectByPrimaryKey(collectionNo);
            if(userCollection!=null)
            {
                return Result.ok().message("查找用户收藏成功").data("userCollection",userCollection);
            }
            else{
                return Result.error().message("查找用户收藏失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户收藏失败");
        }
    }

    @Override
    public Result queryCommunityCollection(int user_no) {
        try {
            List<CommunityInformation> communityInformations=userCollectionMapper.getCommunityCollection(user_no);
            if(communityInformations!=null)
            {
                return Result.ok().message("查找用户收藏社区成功").data("communityInformations",communityInformations);
            }
            else{
                communityInformations = communityInformationMapper.getHotCommunity();
                return Result.ok().message("查找用户收藏社区成功").data("communityInformations",communityInformations);
            }
        }catch (Exception e){
            return Result.error().message("查找用户收藏社区失败");
        }
    }

    @Override
    public Result queryPostCollection(int user_no) {
        try {
            List<Post> posts=userCollectionMapper.getPostCollection(user_no);
            if(posts!=null)
            {
                return Result.ok().message("查找用户收藏帖子成功").data("posts",posts);
            }
            else{
                return Result.error().message("查找用户收藏帖子失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户收藏帖子失败");
        }
    }

    @Override
    public Result queryCommentCollection(int user_no) {
        try {
            List<Comment> comments=userCollectionMapper.getCommentCollection(user_no);
            if(comments!=null)
            {
                return Result.ok().message("查找用户收藏评论成功").data("comments",comments);
            }
            else{
                return Result.error().message("查找用户收藏评论失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户收藏评论失败");
        }
    }

    @Override
    public Result dropAllTypeCollection(int collection_object_no, int collection_object_type) {
        try {

            if(collection_object_type==1)
            {
                userCollectionMapper.deletePostCollection(collection_object_no);
                return Result.ok().message("删除帖子关联收藏成功");
            }
            else if (collection_object_type==2){

                userCollectionMapper.deleteCommentCollection(collection_object_no);
                return Result.ok().message("删除评论关联收藏成功");
            }

            else{
                return Result.error().message("删除帖子关联收藏失败");
            }
        }catch (Exception e){
            return Result.error().message("删除帖子关联收藏失败");
        }
    }

    @Override
    public Result UserCollection(UserCollection record) {
        try {
            UserCollection userCollection = userCollectionMapper.getCollection(record.getUserNo(),record.getCollectionObjectType(),record.getCollectionObjectNo());
            if(userCollection != null )
            {
                if (record.getCollectionObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = communityInformation.getCommunityCollectionNumber()-1;
                    int now1 = communityInformation.getCommunityMember()-1;
                    communityInformation.setCommunityCollectionNumber(now);
                    communityInformation.setCommunityMember(now1);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getCollectionObjectNo(),0);
                    int now2=0;
                    for (Label label:labels)
                    {
                        now2=label.getLabelHot()-10;
                        label.setLabelHot(now2);
                        labelMapper.updateByPrimaryKey(label);
                    }

                }
                if (record.getCollectionObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now =post.getPostCollectionNumber()-1;
                    post.setPostCollectionNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getCollectionObjectNo(),1);
                    int now2=0;
                    for (Label label:labels)
                    {
                        now2=label.getLabelHot()-10;
                        label.setLabelHot(now2);
                        labelMapper.updateByPrimaryKey(label);
                    }
                }
                if (record.getCollectionObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = comment.getCommentCollectionNumber()-1;
                    comment.setCommentCollectionNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                userCollectionMapper.deleteByPrimaryKey(userCollection.getCollectionNo());
                return Result.ok().message("用户删除收藏成功");
            }
            else{
                if (record.getCollectionObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = communityInformation.getCommunityCollectionNumber()+1;
                    int now1 = communityInformation.getCommunityMember()+1;
                    communityInformation.setCommunityCollectionNumber(now);
                    communityInformation.setCommunityMember(now1);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getCollectionObjectNo(),0);
                    int now2=0;
                    for (Label label:labels)
                    {
                        now2=label.getLabelHot()+10;
                        label.setLabelHot(now2);
                        labelMapper.updateByPrimaryKey(label);
                    }

                }
                if (record.getCollectionObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now =post.getPostCollectionNumber()+1;
                    post.setPostCollectionNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getCollectionObjectNo(),1);
                    int now2=0;
                    for (Label label:labels)
                    {
                        now2=label.getLabelHot()+10;
                        label.setLabelHot(now2);
                        labelMapper.updateByPrimaryKey(label);
                    }
                }
                if (record.getCollectionObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(record.getCollectionObjectNo());
                    int now = comment.getCommentCollectionNumber()+1;
                    comment.setCommentCollectionNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                userCollectionMapper.insertSelective(record);
                return Result.ok().message("用户收藏成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("用户操作失败");
        }
    }

    @Override
    public Result UserCollectionCommunity(int user_no, int collectionObjectType, int collectionObjectNo) {
        try {
            UserCollection collection = userCollectionMapper.getCollection(user_no, collectionObjectType, collectionObjectNo);
            return Result.ok().message("操作成功").data("collection",collection);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("操作失败");
        }
    }

}
